﻿<EditDataFormTemplateSpec 
	xmlns="bb_appfx_editdataformtemplate"
	xmlns:common="bb_appfx_commontypes" 
	ID="0dd1ecf6-4cf1-400a-8272-443acefefcdc" 
	Name="Address Custom ID Edit Data Form"
	Description="Provides a mechanism for setting or editing an address's custom ID" 
	Author="Blackbaud Professional Services"
	DataFormInstanceID="e4ed71b1-1890-4ac0-9a0b-50177d5f0b6b" 
	RecordType="Address"
  common:SecurityUIFolder="Service Bus Adapter"
	>

	<SPDataForm>

		<!-- define the edit load SP, which is used to load the data for the form -->
		<LoadImplementation SPName="USR_USP_DATAFORMTEMPLATE_EDITLOAD_ADDRESSCUSTOMID">
			<common:CreateProcedureSQL>
				<![CDATA[
create procedure dbo.USR_USP_DATAFORMTEMPLATE_EDITLOAD_ADDRESSCUSTOMID(
  @ID uniqueidentifier,
  @DATALOADED bit = 0 output,
  @TSLONG bigint = 0 output,
  @CUSTOMID nvarchar(255) = null output
)
as

	set nocount on;
  
  declare @CHANGEAGENTID uniqueidentifier;
  declare @CURRENTDATE datetime = getdate();
  exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output
  
	-- be sure to set these, in case the select returns no rows
	set @DATALOADED = 0
	set @TSLONG = 0

	-- populate the output parameters, which correspond to fields on the form.  Note that
	-- we set @DATALOADED = 1 to indicate that the load was successful.  Otherwise, the system
	-- will display a "no data loaded" message.  Also note that we fetch the TSLONG so that concurrency
	-- can be considered.
  
  if exists (select 1 from dbo.USR_ADDRESS where ID = @ID)
	  select
		  @DATALOADED = 1,
		  @TSLONG = TSLONG,
		  @CUSTOMID = LOOKUPID	
	  from 
      dbo.USR_ADDRESS
	  where 
      ID = @ID
  else
    select
		  @DATALOADED = 1,
		  @CUSTOMID = ''

	return 0;
				]]>
			</common:CreateProcedureSQL>
		</LoadImplementation>

		<SaveImplementation SPName="USR_USP_DATAFORMTEMPLATE_EDIT_ADDRESSCUSTOMID">
			<common:CreateProcedureSQL>
				<![CDATA[
create procedure dbo.USR_USP_DATAFORMTEMPLATE_EDIT_ADDRESSCUSTOMID (
	@ID uniqueidentifier,
	@CHANGEAGENTID uniqueidentifier = null,
	@CUSTOMID nvarchar(255)
)
as

	set nocount on;

	if @CHANGEAGENTID is null  
		exec dbo.USP_CHANGEAGENT_GETORCREATECHANGEAGENT @CHANGEAGENTID output
				
	declare @CURRENTDATE datetime
	set @CURRENTDATE = getdate()

	begin try
		-- handle updating the data
    if exists (select 1 from dbo.USR_ADDRESS where ID = @ID)
      begin
        update 
          dbo.USR_ADDRESS
        set
			    CUSTOMIDENTIFIER = @CUSTOMID,
			    CHANGEDBYID = @CHANGEAGENTID,
			    DATECHANGED = @CURRENTDATE
		    where 
          ID = @ID
      end
    else
      begin
        insert into 
          dbo.USR_ADDRESS 
            (ID, CUSTOMIDENTIFIER, DATEADDED, DATECHANGED, ADDEDBYID, CHANGEDBYID) 
        values 
          (@ID, @CUSTOMID, @CURRENTDATE, @CURRENTDATE, @CHANGEAGENTID, @CHANGEAGENTID)
      end
	end try
	begin catch
		exec dbo.USP_RAISE_ERROR
		return 1
	end catch

return 0;
				]]>
			</common:CreateProcedureSQL>
		</SaveImplementation>

	</SPDataForm>

	<!-- describe fields on the form, which correspond to parameters on the SP.  Note that system paramters 
	like the record ID, DATALOADED, TSLONG, and CURRENTAPPUSERID need not be listed. -->
	<common:FormMetaData FixedDialog="true">
		<common:FormFields>
			<common:FormField FieldID="CUSTOMID" Caption="Custom identifier" DataType="String" MaxLength="255" />
		</common:FormFields>

    <common:WebUIComponent>
      <common:WebUI>
        <common:DefaultWebUI/>
      </common:WebUI>
    </common:WebUIComponent>
    
	</common:FormMetaData>

</EditDataFormTemplateSpec>